package lsprse;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Node
{
   Object val = null;
   List<Node> children = null;

   public Node(Object value)
   {
      if(value instanceof Node)
      {
         int i = 1;
      }
      this.val = value;
   }

   public Node(Object value, List<Node> children)
   {
      this.val = value;
      this.children = children;
   }

   public Node(Object value, Node[] children)
   {
      this.val = value;
      this.children = new ArrayList<Node>(children.length);
      for (Node child : children)
      {
         this.children.add(child);
      }
   }

   public Node(Object o1, Object o2)
   {
      val = o1;
      children = new ArrayList<Node>(1);
      children.add(new Node(o2));
   }

   public Node(Object o1, Node n2)
   {
      val = o1;
      children = new ArrayList<Node>(1);
      children.add(n2);
   }

   public Node(Object o1, Node n1,  Node n2)
   {
      val = o1;
      children = new ArrayList<Node>(2);
      children.add(n1);
      children.add(n2);      
   }

   public Object getValue()
   {
      return val;
   }

   public void setValue(Object value)
   {
      this.val = value;
   }

   public List<Node> getChildren()
   {
      return children;
   }

   public void setChildren(List<Node> children)
   {
      this.children = children;
   }

   public String toString()
   {
      //return val.toString();
      return toXML();
   }
   
   private static final String ELEM = "o";
   private static final String ATTR = "e";
   private static final String Q = "'";
   
   
   
   public String toXML()
   {
      StringBuffer b = new StringBuffer();
      String tabs = "";
      toXML(b, tabs);
      return b.toString();
   }
   
   private void toXML(StringBuffer b, String tabs)
   {
      String xval = (val == null? "list" : val.toString());
      if(children == null)
      {
         b.append(tabs).append('<').append(ELEM).append(' ').append(ATTR)
         .append('=').append(Q).append(xval).append(Q).append("/>"); 
      }
      else
      {
         b.append(tabs).append('<').append(ELEM).append(' ').append(ATTR)
         .append('=').append(Q).append(xval).append(Q).append('>'); 
         b.append('\n');
         for (Node child : children)
         {
            child.toXML(b, tabs + " ");
            b.append('\n');
         }
         b.append(tabs).append("</").append(ELEM).append('>'); 
      }
   }
   
   /**
    * The advantage of putting this to xml is that with a treeviewer editor its much easier
    * to view the tree and collapse away detail etc than just printing something that
    * is bracket separated.
    */
   public void dumpXML(String filePath)
   {
      
      try
      {
         File f= new File(filePath);       
         BufferedWriter out = new BufferedWriter(new FileWriter(f));
         out.write(toXML());
         out.close();
      }
      catch (IOException e)
      {
         throw new RuntimeException(e);
      }
   }
}
